//
//  FiirstPayController.m
//  QRCode
//
//  Created by zhibao on 2022/10/8.
//

#import "FirstPayController.h"
#import "MyReplyController.h"
#import "HomeController.h"
@interface FirstPayController (){
    NSMutableArray *btns;
    NSArray *dataArray;
    NSInteger selectIndex;
    UILabel *princeTitleLabel;
    UIView *payBtnBgView;
}

@end

@implementation FirstPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog(@"[UIDevice currentDevice].userInterfaceIdiom==%ld",(long)[UIDevice currentDevice].userInterfaceIdiom);
    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        [QMUITips showError:@"当前设备暂不支持iPad,请使用iPhone下载该软件使用！"];
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.text = LocalizedString(@"当前设备暂不支持iPad,请使用iPhone下载该软件使用！");
        titleLabel.font = MY_SCALE_SemiboldFONT(21);
        titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        titleLabel.numberOfLines = 0;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = UIColor.blackColor;
        [self.view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(self.view);
        }];
        return;
    }
    dataArray = [NSArray new];
    btns = [NSMutableArray new];
    selectIndex = -1;
    self.gk_navigationBar.hidden = YES;
    [self getData];
}

-(void)getData{
    __weak typeof(self) weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/product/marketing" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"responseObject==%@",responseObject);
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if([responseObject.data[@"data"] count] > 0){
            self->dataArray = responseObject.data[@"data"];
        }
        self->selectIndex = 0;
        [strongSelf initView];
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}

-(void)initView{
    UIView *topbgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MY_Screen_W, 300)];
    [self.view addSubview:topbgView];
    [Helper setDiferentColors:@[(id)MyColor(@"#E3F5FD").CGColor,(id)MyColor(@"#FEFFFF").CGColor] WithView:topbgView withFlag:ColorDirectiondown];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setImage:[UIImage imageNamed:@"com_close"] forState:UIControlStateNormal];
    cancelBtn.frame = CGRectMake(0, NAVIH - 30 , 10 + 22 + 10, 10 + 22 + 10);
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    
    UIImageView *topImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"first_logo"]];
    [self.view addSubview:topImgView];
    [topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.top.equalTo(cancelBtn.mas_bottom);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = LocalizedString(@"升级为高级VIP会员\n解锁全部特权");
    titleLabel.font = MY_SCALE_SemiboldFONT(21);
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = UIColor.blackColor;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImgView.mas_top).offset(26);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-20);
    }];
    
    
    NSArray *contents = @[
        @{@"title":LocalizedString(@"无限制生成照片水印"),@"img":@"first_zhaopian"},
        @{@"title":LocalizedString(@"无限制生成视频水印"),@"img":@"first_shipin"},
        @{@"title":LocalizedString(@"无限制生成拼接图片水印"),@"img":@"first_pinjie"},
        @{@"title":LocalizedString(@"本地相册上传生成水印"),@"img":@"first_bendi"},
    ];
    
    UIView *tempTopView;
    for (int i = 0 ; i < contents.count; i++) {
        UILabel *tipLabel = [[UILabel alloc]init];
        tipLabel.text = contents[i][@"title"];
        tipLabel.font = MY_SCALE_RegularFONT(14);
        tipLabel.lineBreakMode = NSLineBreakByWordWrapping;
        tipLabel.numberOfLines = 0;
        tipLabel.textColor = UIColor.blackColor;
        [self.view addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if(tempTopView){
                make.top.equalTo(tempTopView.mas_bottom).offset(27);
            }else{
                make.top.equalTo(titleLabel.mas_bottom).offset(37);
            }
            make.left.mas_equalTo(62);
            make.right.mas_equalTo(-30);
        }];
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:contents[i][@"img"]]];
        [self.view addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.centerY.equalTo(tipLabel);
        }];
        tempTopView = tipLabel;
    }
    
    UIButton *repayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [repayBtn setTitleColor:MyColor(@"#9AB6C8") forState:UIControlStateNormal];
    [repayBtn setTitle:LocalizedString(@"恢复购买") forState:UIControlStateNormal];
    repayBtn.titleLabel.font = MY_SCALE_RegularFONT(12);
    [repayBtn addTarget:self action:@selector(repalyClcik) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:repayBtn];
    [repayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cancelBtn);
        make.right.equalTo(self.view).offset(-10);
    }];
    
    YYLabel *bottomLabel = [[YYLabel alloc]initWithFrame:CGRectMake(0, MY_Screen_H - SafeBottomMargin - 20 - 40, MY_Screen_W, 20)];
    bottomLabel.centerX = MY_Screen_W / 2;
    
    //bottomLabel.preferredMaxLayoutWidth = MY_Screen_W - 60;
    [self.view addSubview:bottomLabel];
    
    NSMutableString *string = [[NSMutableString alloc]init];
    [string appendString:LocalizedString(@"用户协议")];
    [string appendString:@"          "];
    [string appendString:LocalizedString(@"隐私协议")];
    [string appendString:@"          "];
    [string appendString:LocalizedString(@"订阅协议")];
    NSRange yhrange = [string rangeOfString:LocalizedString(@"用户协议")];
    NSRange ysrange = [string rangeOfString:LocalizedString(@"隐私协议")];
    NSRange dyrange = [string rangeOfString:LocalizedString(@"订阅协议")];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:string];
    
    attri.yy_font = MY_SCALE_RegularFONT(11);
    attri.yy_color = MyColor(@"999999");
    __weak typeof(self) weakSelf = self;
    [attri yy_setTextHighlightRange:NSMakeRange(yhrange.location, yhrange.length) color:MyColor(@"999999") backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"用户协议") WithUrl:@"https://docs.qq.com/doc/DRll6a01QaWNiclZE"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    YYTextDecoration *yhdecoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@(0.5) color:MyColor(@"999999")];
    [attri yy_setTextUnderline:yhdecoration range:yhrange];
    
    [attri yy_setTextHighlightRange:NSMakeRange(ysrange.location, ysrange.length) color:MyColor(@"999999") backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"隐私协议") WithUrl:@"https://docs.qq.com/doc/DRmxCb3NSTUF2YnFm"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    YYTextDecoration *ysdecoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@(0.5) color:MyColor(@"999999")];
    [attri yy_setTextUnderline:ysdecoration range:ysrange];
    
    [attri yy_setTextHighlightRange:NSMakeRange(dyrange.location, dyrange.length) color:MyColor(@"999999") backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"订阅协议") WithUrl:@"https://docs.qq.com/doc/DRmlaV0lxYlhjU1Bw"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    YYTextDecoration *dydecoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@(0.5) color:MyColor(@"999999")];
    [attri yy_setTextUnderline:dydecoration range:dyrange];
    bottomLabel.attributedText = attri;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    
    UIView *tempView;
    UIButton *cotiueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cotiueBtn.frame = CGRectMake(30, bottomLabel.top - 22 - 45, MY_Screen_W - 60, 40);
    cotiueBtn.backgroundColor = MYBlue;
    cotiueBtn.layer.cornerRadius = 22;
    [cotiueBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [cotiueBtn setTitle:LocalizedString(@"继续") forState:UIControlStateNormal];
    cotiueBtn.titleLabel.font = MY_SCALE_SemiboldFONT(16);
    [cotiueBtn addTarget:self action:@selector(checkLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cotiueBtn];
    tempView = cotiueBtn;
    NSString *onlyPay = [DWKeychainTool getItemForKey:@"onlypay1"];
    
    if(!onlyPay){
        payBtnBgView = [[UIView alloc]initWithFrame:CGRectMake(30, cotiueBtn.top - 24 - 40, MY_Screen_W - 60, 40)];
        payBtnBgView.layer.cornerRadius = 20;
        payBtnBgView.backgroundColor = MyColor(@"#ECFAFF");
        [self.view addSubview:payBtnBgView];
        
        UILabel *payTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 300, 40)];
        payTitleLabel.text = LocalizedString(@"试⽤已开启");
        payTitleLabel.tag = 10000;
        payTitleLabel.textColor = MYBlue;
        payTitleLabel.font = MY_SCALE_SemiboldFONT(12);
        [payBtnBgView addSubview:payTitleLabel];
        
        UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        payBtn.frame = CGRectMake(payBtnBgView.width - 15 - 34, 0, 34, payBtnBgView.height);
        [payBtn setImage:[UIImage imageNamed:@"pay_open"] forState:UIControlStateNormal];
        payBtn.tag = 100;
        [payBtn addTarget:self action:@selector(payBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [payBtnBgView addSubview:payBtn];
        tempView = payBtnBgView;
    }
    
    princeTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, tempView.top - 75 - 20, MY_Screen_W - 20, 75)];
    princeTitleLabel.font = MY_SCALE_RegularFONT(12);
    princeTitleLabel.textAlignment = NSTextAlignmentCenter;
    princeTitleLabel.numberOfLines = 0;
    princeTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    if(dataArray.count > 0){
        if(onlyPay){
            [Helper setLabelInfo:@[
                @[LocalizedString(@"￥98/年"),LocalizedString(@"\n连续订阅,可随时取消")],
                @[MYBlue,MyColor(@"#999999")],
                @[MY_SCALE_SemiboldFONT(18),MY_SCALE_SemiboldFONT(17)]
            ] withLabel:princeTitleLabel];
            selectIndex = 1;
        }else{
            NSDictionary *dic = dataArray[0];
            if([dic[@"mode"] intValue] == 1){
                NSString *temp = [NSString stringWithFormat:@"%@天试用,之后",dic[@"mode_value"]];
                [Helper setLabelInfo:@[
                    @[LocalizedString(temp),LocalizedString(@"￥18/周"),LocalizedString(@"\n连续订阅,可随时取消")],
                    @[MyColor(@"#333333"),MYBlue,MyColor(@"#999999")],
                    @[MY_SCALE_SemiboldFONT(18),MY_SCALE_SemiboldFONT(18),MY_SCALE_RegularFONT(17)]
                ] withLabel:princeTitleLabel];
            }else{
                [Helper setLabelInfo:@[
                    @[LocalizedString(@"3天试用,之后"),LocalizedString(@"￥18/周"),LocalizedString(@"\n连续订阅,可随时取消")],
                    @[MyColor(@"#333333"),MYBlue,MyColor(@"#999999")],
                    @[MY_SCALE_SemiboldFONT(18),MY_SCALE_SemiboldFONT(18),MY_SCALE_RegularFONT(17)]
                ] withLabel:princeTitleLabel];
            }
        }
    }
    [self.view addSubview:princeTitleLabel];
}

-(void)payBtnClick:(UIButton *)sender{
    UILabel *tempLabel = [sender.superview viewWithTag:10000];
    
    if(sender.tag == 100){
        sender.tag = 101;
        [sender setImage:[UIImage imageNamed:@"pay_close"] forState:UIControlStateNormal];
        tempLabel.text = LocalizedString(@"试⽤已关闭");
        tempLabel.textColor = MyColor(@"999999");
        payBtnBgView.backgroundColor = MyColor(@"EDEDED");
        [Helper setLabelInfo:@[
            @[LocalizedString(@"￥98/年"),LocalizedString(@"\n连续订阅,可随时取消")],
            @[MYBlue,MyColor(@"#999999")],
            @[MY_SCALE_SemiboldFONT(18),MY_SCALE_RegularFONT(17)]
        ] withLabel:princeTitleLabel];
    }else{
        sender.tag = 100;
        [sender setImage:[UIImage imageNamed:@"pay_open"] forState:UIControlStateNormal];
        tempLabel.text = LocalizedString(@"试⽤已开启");
        payBtnBgView.backgroundColor = MyColor(@"#ECFAFF");
        tempLabel.textColor = MYBlue;
        NSDictionary *dic = dataArray[0];
        if([dic[@"mode"] intValue] == 1){
            NSString *temp = [NSString stringWithFormat:@"%@天试用,之后",dic[@"mode_value"]];
            [Helper setLabelInfo:@[
                @[LocalizedString(temp),LocalizedString(@"￥18/周"),LocalizedString(@"\n连续订阅,可随时取消")],
                @[MyColor(@"#333333"),MYBlue,MyColor(@"#999999")],
                @[MY_SCALE_SemiboldFONT(18),MY_SCALE_SemiboldFONT(18),MY_SCALE_RegularFONT(17)]
            ] withLabel:princeTitleLabel];
        }else{
            [Helper setLabelInfo:@[
                @[LocalizedString(@"3天试用,之后"),LocalizedString(@"￥18/周"),LocalizedString(@"\n连续订阅,可随时取消")],
                @[MyColor(@"#333333"),MYBlue,MyColor(@"#999999")],
                @[MY_SCALE_SemiboldFONT(18),MY_SCALE_SemiboldFONT(18),MY_SCALE_RegularFONT(17)]
            ] withLabel:princeTitleLabel];
        }
    }
    selectIndex = sender.tag - 100;
}

-(void)cancelClick{
    [[NSUserDefaults standardUserDefaults] setValue:@"first" forKey:WelcomeFlag];
    [[NSUserDefaults standardUserDefaults] synchronize];
    HomeController *controller = [[HomeController alloc] init];
    LNavigationViewController *nav = [LNavigationViewController rootVC:controller];
    nav.gk_openScrollLeftPush = YES;
    nav.gk_openSystemNavHandle = YES;
    MYAPP.window.rootViewController = nav;
}

-(void)repalyClcik{
    [QMUITips showLoading:LocalizedString(@"查询中...") inView:self.view];
    [[IAPShare sharedHelper].iap restoreProductsWithCompletion:^(SKPaymentQueue *payment, NSError *error) {
        
        //check with SKPaymentQueue
        
        // number of restore count
        int numberOfTransactions = payment.transactions.count;
        [QMUITips hideAllTips];
        if(numberOfTransactions < 1){
            [QMUITips showError:LocalizedString(@"无可恢复购买的项目")];
            return;
        }
        DLog(@"numberOfTransactions==%d",numberOfTransactions);
        BOOL  flag = NO;
        for (SKPaymentTransaction *transaction in payment.transactions)
        {
            if(transaction.transactionState == SKPaymentTransactionStateRestored && flag == NO){
                flag = YES;
                NSString *purchased = transaction.payment.productIdentifier;
                DLog(@"purchased==%@",purchased);
                [self verifyPurchaseWithPaymentTransaction:transaction];
            }
        }
    }];
}
- (void)verifyPurchaseWithPaymentTransaction:(SKPaymentTransaction *)transaction{
    //交易验证
    [QMUITips showLoading:LocalizedString(@"正在恢复购买...") inView:self.view];
    NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
    if(!receipt){
        return;
    }
    DLog(@"data==%@",[receipt base64EncodedStringWithOptions:0]);
    NSDictionary *paras =@{
        @"receipt":[receipt base64EncodedStringWithOptions:0]
    };
    [[HttpHelper sharedInstance] sendPost:@"api/pay/restore" parameters:paras success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"payfinish===%@",responseObject.data);
        if (responseObject.code == 1) {
            [[UserInfoManager sharedManager] getUserInfoData:^(BOOL flag) {
                [QMUITips hideAllTips];
                [QMUITips showSucceed:LocalizedString(@"恢复成功")];
                HomeController *controller = [[HomeController alloc] init];
                LNavigationViewController *nav = [LNavigationViewController rootVC:controller];
                nav.gk_openScrollLeftPush = YES;
                nav.gk_openSystemNavHandle = YES;
                MYAPP.window.rootViewController = nav;
            }];
            
        }else{
            [QMUITips hideAllTips];
            [QMUITips showError:LocalizedString(@"无可恢复购买的项目")];
        }
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        [QMUITips hideAllTips];
        [QMUITips showError:LocalizedString(@"无可恢复购买的项目")];
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }];
}

-(void)checkLogin{
    [self gotoPay];
}

-(void)gotoPay{
    if(selectIndex < 0){
        return;
    }
    //    NSDictionary *dic = dataArray[selectIndex];
    //检查是否可以购买
    __weak typeof(self) weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/pay/check" parameters:@{@"productId":dataArray[selectIndex][@"product_id"]} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(responseObject.code == 1){
            [QMUITips showLoading:LocalizedString(@"购买中...") inView:self.view];
            [[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
             {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    if(response > 0 ) {
                        SKProduct* product = nil;
                        for(SKProduct *pro in response.products){
                            if([pro.productIdentifier isEqualToString:self->dataArray[self->selectIndex][@"product_id"]]){
                                NSLog(@"Price: %@",[[IAPShare sharedHelper].iap getLocalePrice:pro]);
                                NSLog(@"Title: %@",pro.localizedTitle);
                                product = pro;
                                break;
                            }
                        }
                        
                        if(!product){
                            [QMUITips hideAllTipsInView:strongSelf.view];
                            [QMUITips showError:LocalizedString(@"无可购买项目")];
                            return;
                        }
                        [[IAPShare sharedHelper].iap buyProduct:product
                                                   onCompletion:^(SKPaymentTransaction* trans){
                            
                            if(trans.error)
                            {
                                NSLog(@"Fail %@",[trans.error localizedDescription]);
                                [QMUITips hideAllTipsInView:strongSelf.view];
                            }else if(trans.transactionState == SKPaymentTransactionStatePurchased) {
                                
                                //交易验证
                                NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
                                NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
                                if(!receipt){
                                    [QMUITips hideAllTipsInView:strongSelf.view];
                                    return;
                                }
                                NSDictionary *paras =@{
                                    @"receipt":[receipt base64EncodedStringWithOptions:0],
                                    @"productId":self->dataArray[self->selectIndex][@"product_id"],
                                    @"transactionId":trans.transactionIdentifier
                                };
                                DLog(@"paras==%@",paras);
                                [DWKeychainTool saveItem:[receipt base64EncodedStringWithOptions:0] key:@"onlypay1"];
                                [[HttpHelper sharedInstance] sendPost:@"api/pay/subscribe" parameters:paras success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
                                    DLog(@"payfinish===%@",responseObject.data);
                                    [QMUITips hideAllTipsInView:strongSelf.view];
                                    if (responseObject.code == 1) {
                                        [QMUITips showSucceed:LocalizedString(@"购买成功")];
                                        [[UserInfoManager sharedManager] getUserInfoData:^(BOOL flag) {
                                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                HomeController *controller = [[HomeController alloc] init];
                                                LNavigationViewController *nav = [LNavigationViewController rootVC:controller];
                                                nav.gk_openScrollLeftPush = YES;
                                                nav.gk_openSystemNavHandle = YES;
                                                MYAPP.window.rootViewController = nav;
                                                
                                            });
                                        }];
                                    }else{
                                        [QMUITips showError:LocalizedString(@"已购买过或暂时无法购买")];
                                    }
                                    [[SKPaymentQueue defaultQueue] finishTransaction:trans];
                                } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
                                    [QMUITips hideAllTipsInView:strongSelf.view];
                                    [QMUITips showSucceed:LocalizedString(@"购买失败")];
                                    [[SKPaymentQueue defaultQueue] finishTransaction:trans];
                                }];
                                
                            }else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                                [QMUITips hideAllTipsInView:strongSelf.view];
                                NSLog(@"Fail");
                            }
                        }];//end of buy product
                    }
                });
            }];
        }else{
            [QMUITips showError:LocalizedString(@"已购买过或暂时无法购买")];
        }
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
    
}


@end
